package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import com.lingnan.entity.po.Company;
import com.lingnan.entity.po.CompanyHr;
import com.lingnan.entity.po.JobSeeker;
import com.lingnan.entity.po.UserAccount;
import com.lingnan.mapper.CompanyHrMapper;
import com.lingnan.mapper.CompanyMapper;
import com.lingnan.mapper.JobSeekerMapper;
import com.lingnan.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl {
    
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    
    @Autowired
    private CompanyMapper companyMapper;
    
    @Autowired
    private CompanyHrMapper companyHrMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户账户ID查询用户信息
     */
    public UserAccount getUserAccountById(Long userId) {
        return userMapper.selectOne(new LambdaQueryWrapper<UserAccount>()
                .eq(UserAccount::getId, userId));
    }
    /**
     * 根据用户账户ID查询求职者信息
     */
    public JobSeeker getJobSeekerByUserId(Long userId) {
        return jobSeekerMapper.selectOne(new LambdaQueryWrapper<JobSeeker>()
                .eq(JobSeeker::getUserId, userId));
    }
    
    /**
     * 根据用户账户ID查询公司信息
     */
    public Company getCompanyByUserId(Long userId) {
        return companyMapper.selectOne(new LambdaQueryWrapper<Company>()
                .eq(Company::getUserId, userId));
    }
    
    /**
     * 根据用户账户ID查询HR信息
     */
    public CompanyHr getHrByUserId(Long userId) {
        return companyHrMapper.selectOne(new LambdaQueryWrapper<CompanyHr>()
                .eq(CompanyHr::getUserAccountId, userId));
    }
    
    /**
     * 根据公司ID查询该公司的所有HR
     */
    public List<CompanyHr> getHrsByCompanyId(Long companyId) {
        return companyHrMapper.selectList(new LambdaQueryWrapper<CompanyHr>()
                .eq(CompanyHr::getCompanyId, companyId));
    }
    
    /**
     * 根据HR的用户账户ID查询对应的公司信息
     */
    public Company getCompanyByHrUserId(Long userId) {
        // 先根据用户ID查询HR信息
        CompanyHr hr = companyHrMapper.selectOne(new LambdaQueryWrapper<CompanyHr>()
                .eq(CompanyHr::getUserAccountId, userId));
        
        if (hr == null) {
            return null;
        }
        
        // 再根据HR的公司ID查询公司信息
        return companyMapper.selectOne(new LambdaQueryWrapper<Company>()
                .eq(Company::getId, hr.getCompanyId()));
    }

    public List<UserAccount> getallUser() {
        return userMapper.selectList(new QueryWrapper<>());
    }

    public Object update(UserAccount userAccount) {
        return userMapper.update(new UpdateWrapper<>(userAccount));
    }

    /**
     * 分页查询用户列表
     * @param page 分页参数
     * @param username 用户名
     * @param email 邮箱
     * @param userType 用户类型
     * @param status 状态
     * @return 分页结果
     */
    public IPage<UserAccount> getUserList(Page<UserAccount> page, String username, String email, Integer userType, Integer status) {
        LambdaQueryWrapper<UserAccount> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据用户名模糊查询
        if (StringUtils.hasText(username)) {
            queryWrapper.like(UserAccount::getUsername, username);
        }
        
        // 根据邮箱模糊查询
        if (StringUtils.hasText(email)) {
            queryWrapper.like(UserAccount::getEmail, email);
        }
        
        // 根据用户类型查询
        if (userType != null) {
            queryWrapper.eq(UserAccount::getUserType, userType);
        }
        
        // 根据状态查询
        if (status != null) {
            queryWrapper.eq(UserAccount::getStatus, status);
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(UserAccount::getCreatedAt);
        
        return userMapper.selectPage(page, queryWrapper);
    }
}